package com.atguigu.java.ai.langchain4j.utils;

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.time.Duration;
import java.util.Base64;
import java.util.Collections;

/**
 * @author: chengzhzh@atguigu.cn
 * @date: 2023/5/5 14:01
 * @desc: OpenCV 图片点击工具类
 *
 */
public class ImageClickUtils {
    static {
        //加载OpenCV动态库
        System.load("C:\\project\\java-ai-langchain4j\\src\\main\\resources\\opencv_java4110.dll");
    }

    /**
     * 根据模板图片进行点击
     * @param driver  驱动
     * @param templatePath  图片路径
     *
     */
    public static void clickByImage(RemoteWebDriver driver, String templatePath) {
        // 1. 获取当前屏幕截图(Base64方式)
        String screenshotBase64 = driver.getScreenshotAs(OutputType.BASE64);
        byte[] screenshotBytes = Base64.getDecoder().decode(screenshotBase64);
        Mat screenMat = Imgcodecs.imdecode(new MatOfByte(screenshotBytes), Imgcodecs.IMREAD_COLOR);

        // 2. 加载模板图片
        Mat template = Imgcodecs.imread(templatePath, Imgcodecs.IMREAD_COLOR);

        // 3. 执行模板匹配
        Mat result = new Mat();
        Imgproc.matchTemplate(screenMat, template, result, Imgproc.TM_CCOEFF_NORMED);

        // 4. 获取最佳匹配位置
        Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
        Point matchLoc = mmr.maxLoc;

        // 5. 计算点击坐标（考虑状态栏偏移）
        int x = (int) (matchLoc.x + template.cols() / 2);
        int y = (int) (matchLoc.y + template.rows() / 2 );

        // 6. 执行点击操作
        PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
        Sequence tap = new Sequence(finger, 0)
                .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), x, y))
                .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
                .addAction(new Pause(finger, Duration.ofMillis(100)))
                .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
        driver.perform(Collections.singletonList(tap));
    }
}
